﻿using Aspose.Cells;
using Aspose.Words;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace FlexFlow
{
    /// <summary>
    /// 表示工作流引擎表单转换器
    /// </summary>
    public class WorkFlowFormConventer
    {
        FunctionBuilder fb;

        /// <summary>
        /// 将Excel文件转换为Cshtml文件
        /// </summary>
        /// <param name="excelFilePath">
        /// Excel源文件
        /// </param>
        /// <param name="cshtmlFilePath">
        /// cshtml文件
        /// </param>
        public void ConventExcelToCshtml(string excelFilePath, string cshtmlFilePath)
        {
            HtmlSaveOptions sv = new HtmlSaveOptions(Aspose.Cells.SaveFormat.Html);
            sv.CreateDirectory = true;
            sv.ExportFrameScriptsAndProperties = false;
            sv.ExportActiveWorksheetOnly = true;
            sv.HtmlCrossStringType = HtmlCrossType.Cross;
            sv.Encoding = System.Text.Encoding.Unicode;
            MemoryStream ms = new MemoryStream();
            Workbook wb = new Workbook(excelFilePath);
            FunctionBuilder fb = new FunctionBuilder();
            foreach (var ws in wb.Worksheets)
            {
                foreach (Cell cell in ws.Cells)
                {
                    string val = cell.StringValue;
                    cell.Value = fb.BuildFunc(val);
                }
            }
            wb.Save(ms, sv);
            string resContents = fb.EndBuild() + Encoding.Unicode.GetString(ms.ToArray());
            ms.Close();
            resContents = resContents.Replace("%{", "@{").Replace("}%", "}");
            File.WriteAllText(cshtmlFilePath, resContents);
        }
        /// <summary>
        /// 遍历结点以及其子节点
        /// </summary>
        /// <param name="node">
        /// 要遍历的节点
        /// </param>
        public void EmuNode(Node node)
        {

            if (node.NodeType == NodeType.Cell)
            {
                Aspose.Words.Tables.Cell cel = node as Aspose.Words.Tables.Cell;
                DocumentBuilder db = new DocumentBuilder((Document)cel.Document);
                string val = node.GetText();
                if (fb.IsFunc(val))
                {
                    cel.RemoveAllChildren();
                    Paragraph par = new Paragraph(node.Document);
                    cel.Paragraphs.Add(par);
                    db.MoveTo(par);
                    db.Write(fb.BuildFunc(val));
                }
            }

            CompositeNode cnode = node as CompositeNode;
            if (cnode != null)
            {
                foreach (Node enode in cnode)
                {
                    EmuNode(enode);
                }
            }

        }
        /// <summary>
        /// 将Word文档转换成cshtml文件
        /// </summary>
        /// <param name="doc">
        /// Word文档
        /// </param>
        /// <param name="cshtmlFilePath">
        /// cshtml文件
        /// </param>
        public void ConventWordDocumentToCsHtml(Document doc,string cshtmlFilePath)
        {
            Encoding enc = Encoding.Default;
            Aspose.Words.Saving.HtmlSaveOptions sv = new Aspose.Words.Saving.HtmlSaveOptions(Aspose.Words.SaveFormat.Html);
            sv.Encoding = enc;
            MemoryStream ms = new MemoryStream();
            fb = new FunctionBuilder();
            EmuNode(doc);
            doc.Save(ms, sv);

            string resContents = fb.EndBuild() + enc.GetString(ms.ToArray());
            ms.Close();
            resContents = resContents.Replace("%{", "@{").Replace("}%", "}");
            File.WriteAllText(cshtmlFilePath, resContents, enc);
        }
        /// <summary>
        /// 将指定路径的Word文档转换成cshtml文件
        /// </summary>
        /// <param name="wordFilePath">
        /// Word文件路径
        /// </param>
        /// <param name="cshtmlFilePath">
        /// 转换后的cshtml文件存放路径
        /// </param>
        public void ConventWordFileToCsHtml(string wordFilePath, string cshtmlFilePath)
        {
            ConventWordDocumentToCsHtml(new Document(wordFilePath), cshtmlFilePath);
        }
        /// <summary>
        /// 将指定文件流中的Word文档转换成cshtml文件
        /// </summary>
        /// <param name="fileStream">
        /// Word文件流
        /// </param>
        /// <param name="cshtmlFilePath">
        /// 转换后的cshtml文件存放路径
        /// </param>
        public void ConventWordStreamToCsHtml(Stream fileStream,string cshtmlFilePath)
        {
            ConventWordDocumentToCsHtml(new Document(fileStream), cshtmlFilePath);
        }

        static WorkFlowFormConventer()
        {
            new Aspose.Words.License().SetLicense(LicenseHelper.License.LStream);
        }
    }
}
